/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.basedata.category.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BaseCategoryMapper;
import com.nmf.dao.gen.bean.BaseCategory;
import com.nmf.dao.gen.bean.BaseCategoryExample;
import com.nmf.site.basedata.category.enums.CategoryType;
import com.nmf.site.common.utils.IdGenerater;

/**
 * 基础分类Service
 * @author 北冥
 * @version 2018-12-28
 */
@Service
@Transactional(readOnly = true)
public class BaseBizCategoryService{

	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public List<BaseCategory> queryCategory(CategoryType categoryType) {
		BaseCategoryExample baseCategoryExample = new BaseCategoryExample();
		BaseCategoryExample.Criteria criteria = baseCategoryExample.createCriteria();
		
		criteria.andCategoryTypeEqualTo(categoryType.value);
		
		List<BaseCategory> dlist = baseCategoryMapper.selectByExample(baseCategoryExample);
		setParentCategory(dlist);
		return dlist;
	}
	
	
	public void clearParentCategory(BaseCategory baseCategory) {
		if(baseCategory.getParentCategory()==null || StringUtils.isBlank(baseCategory.getParentCategory().getId())) {
			baseCategory.setParentId("");	
		}
	}
	
	public void setParentCategory(BaseCategory baseCategory) {
		if(StringUtils.isNotEmpty(baseCategory.getParentId())) {
			
			BaseCategory baseParentCategory = baseCategoryMapper.selectByPrimaryKey(baseCategory.getParentId());
			baseCategory.setParentCategory(baseParentCategory);
			
		}
		if(baseCategory.getParentCategory()!=null && StringUtils.isNotEmpty(baseCategory.getParentCategory().getId())) {
			baseCategory.setParentId(baseCategory.getParentCategory().getId());
		}
	}
	
	
	public void setParentCategory(List<BaseCategory> baseCategoryList) {
		for(BaseCategory baseCategory : baseCategoryList) {
			setParentCategory(baseCategory);
		}
	}
}